package com.xbongbong.parent.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicAddLinkData;
import com.xbongbong.saas.domain.entity.DynamicEntity;
import com.xbongbong.saas.domain.entity.competitor.SimpleDynamicEntity;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.base.CommunicateBaseEnum;
import com.xbongbong.saas.model.DynamicModel;
import com.xbongbong.saas.model.SimpleDynamicModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2023/01/05 15:57
 */
@Component
@Slf4j
public abstract class AbstractBusinessDynamicStrategy {

    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private SimpleDynamicModel simpleDynamicModel;
    @Resource
    private DynamicModel dynamicModel;

    /**
     * 新增关联数据
     * date: 2022/5/23 2:34 下午
     * @param list:
     */
    @Async("dynamicThreadPool")
    public void addLinkData(List<DynamicAddLinkData> list){
        try {
            if(CollectionUtils.isEmpty(list)){
                return;
            }
            DynamicAddLinkData dynamicAddLinkData = list.get(0);
            String paasFormName = dynamicAddLinkData.getPaasFormName();
            String userId = dynamicAddLinkData.getUserId();
            String corpid = dynamicAddLinkData.getCorpid();
            String name = dynamicAddLinkData.getName();
            String userName = dynamicAddLinkData.getUserName();
            Long linkDataFormId = dynamicAddLinkData.getFormId();
            Long dataId = dynamicAddLinkData.getDataId();

            if(StringUtil.isEmpty(paasFormName)){
                PaasFormEntity paasFormEntity =  paasFormModel.getByKey(linkDataFormId, corpid);
                paasFormName = paasFormEntity.getName();
            }
            List<DynamicEntity> dynamicEntityList = new ArrayList<>();

            List<SimpleDynamicEntity> competitorDynamicList = new ArrayList<>();

            String finalPaasFormName = paasFormName;
            list.forEach(temp->{
                Integer parentBusinessType = temp.getParentBusinessType();
                Long parentDataId = temp.getParentDataId();
                String memo;
                String info = name;
                if (Objects.equals(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(), temp.getBusinessType())) {
                    memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.DYNAMIC_USER_COMMUNICATE_ADD),
                            userName);
                    JSONObject addNewData = temp.getAddNewData();
                    info = addNewData.getString(CommunicateBaseEnum.MEMO.getAttr());
                } else {
                    memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.DYNAMIC_LINK_DATA_ADD),
                            userName, finalPaasFormName, name);
                }
                DynamicEntity dynamicEntity = new DynamicEntity(corpid,dataId,temp.getBusinessType(),info,memo,
                        userId, ProjectLogEnum.PAAS_ADD.getSubType());
                dynamicSetId(parentBusinessType,parentDataId,dynamicEntity);
                //竞争对手动态插入到简单动态表中
                if (Objects.equals(temp.getBusinessType(), XbbRefTypeEnum.COMPETITOR.getCode())) {
                    competitorDynamicList.add(convertDynamic(dynamicEntity));
                }
                dynamicEntityList.add(dynamicEntity);
            });

            if (CollectionUtils.isNotEmpty(competitorDynamicList)) {
                simpleDynamicModel.insertBatch(competitorDynamicList);
            }

            dynamicModel.insertBatch(dynamicEntityList);
        }catch (Exception e){
            log.error("com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy.addLinkData",e);
        }
    }


    protected void dynamicSetId(Integer parentBusinessType,Long parentDataId,DynamicEntity dynamicEntity){
        if(Objects.equals(parentBusinessType,XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode())){
            dynamicEntity.setCustomerId(parentDataId);
        } else if(Objects.equals(parentBusinessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())){
            dynamicEntity.setOpportunityId(parentDataId);
        } else if(Objects.equals(parentBusinessType,XbbRefTypeEnum.CONTACT.getCode())){
            dynamicEntity.setContactId(parentDataId);
        } else if(Objects.equals(parentBusinessType,XbbRefTypeEnum.CONTRACT.getCode())){
            dynamicEntity.setContractId(parentDataId);
        }else if (Objects.equals(parentBusinessType,XbbRefTypeEnum.COMPETITOR.getCode())){
            dynamicEntity.setDataId(parentDataId);
        }
    }
    /**
     * 将动态转化为简单动态
     * @param dynamicEntity
     * @return
     */
    public SimpleDynamicEntity convertDynamic(DynamicEntity dynamicEntity){
        SimpleDynamicEntity simpleDynamicEntity = new SimpleDynamicEntity();
        BeanUtil.copyProperties(dynamicEntity,simpleDynamicEntity);
        simpleDynamicEntity.setDataId(dynamicEntity.getDataId());
        return simpleDynamicEntity;
    }
}
